package com.questetra.OAuth.core
{
	import flash.utils.ByteArray;
 
	public class MultiPartData
	{
		private const N:String = '\r\n';
		private var _boundary:String;
		private var _data:ByteArray;
 
		public function MultiPartData(boundary:String = '-------------------------7d76d1b56035e'){
			_boundary = boundary;
			_data = new ByteArray();
		}

		public function addString( name:String, value:String):void{
			var str:String;
			str = '--'+_boundary+N;
			str += "Content-Disposition: form-data; name=\""+name+ "\""+N+N;
			str += value + N;

			_data.writeMultiByte(str, 'utf-8');
		}
		public function addBynary( name:String, value:ByteArray, filename:String, type:String = 'application/octet-stream'):void{
			var str:String;
			str = '--'+_boundary+N;
			str += "Content-Disposition: form-data; name=\""+name+ "\"; filename=\""+ filename +"\""+N;
			str += "Content-Type: " + type +""+ N + N;

			_data.writeMultiByte(str, 'utf-8');
			_data.writeBytes(value, 0, value.length);
			_data.writeMultiByte(N, 'ascii');
		}
		public function resetData():void{
			_data = new ByteArray();
		}
		public function get data():ByteArray{
			var ret:ByteArray = new ByteArray();
			ret.writeBytes(_data, 0, _data.length);
			ret.writeMultiByte('--'+_boundary+'--', 'ascii');
			return ret;
		}
		public function get boundary():String{
			return _boundary;
		}
		public function set boundary(value:String):void{
			_boundary = value;
		}
	}
}